home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_1.3 / Read-Me1.3 / Printer1.3 / Driver.Examples / src / epsonX / render.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-08-01  |  8.3 KB  |  323 lines

  1. /*
  2.     EpsonX (EX/FX/JX/LX/MX/RX) driver.
  3.     David Berezowski - October/87.
  4. */
  5.  
  6. #include <exec/types.h>
  7. #include <exec/nodes.h>
  8. #include <exec/lists.h>
  9. #include <exec/memory.h>
  10. #include "../printer/printer.h"
  11. #include "../printer/prtbase.h"
  12.  
  13. #define NUMSTARTCMD    7    /* # of cmd bytes before binary data */
  14. #define NUMENDCMD    1    /* # of cmd bytes after binary data */
  15. #define NUMTOTALCMD     (NUMSTARTCMD + NUMENDCMD)    /* total of above */
  16. #define NUMLFCMD    4    /* # of cmd bytes for linefeed */
  17. #define MAXCOLORBUFS    4    /* max # of color buffers */
  18.  
  19. #define STARTLEN    19
  20. #define PITCH        1
  21. #define CONDENSED    2
  22. #define LMARG        8
  23. #define RMARG        11
  24. #define DIREC        15
  25.  
  26. static ULONG TwoBufSize;
  27. static UWORD RowSize, ColorSize, NumColorBufs, dpi_code, spacing;
  28. static UWORD colorcodes[MAXCOLORBUFS];
  29.  
  30. Render(ct, x, y, status)
  31. long ct, x, y, status;
  32. {
  33.     extern void *AllocMem(), FreeMem();
  34.  
  35.     extern struct PrinterData *PD;
  36.     extern struct PrinterExtendedData *PED;
  37.  
  38.     UBYTE *CompactBuf();
  39.     static ULONG BufSize, TotalBufSize, dataoffset;
  40.     static UWORD spacing, colors[MAXCOLORBUFS];
  41.     /*
  42.         00-01    \003P        set pitch (10 or 12 cpi)
  43.         02-02    \022        set condensed fine (on or off)
  44.         03-05    \033W\000    enlarge off
  45.         06-08    \033ln        set left margin to n
  46.         09-11    \033Qn        set right margin to n
  47.         12-12    \015        carriage return
  48.         13-15    \033U1        set uni-directional mode
  49.         16-18    \033t\000    see kludge note below
  50.         Kludge to get this to work on a CBM_MPS-1250  which interprets
  51.         'ESCr' as go into reverse print mode.  The 'ESCt' tells it to
  52.         get out of reverse print mode.  The 'NULL' is ignored by the
  53.         CBM_MPS-1250 and required by all Epson printers as the
  54.         terminator for the 'ESCtNULL' command which means select
  55.         normal char set (which has no effect).
  56.     */
  57.     static UBYTE StartBuf[STARTLEN] =
  58.         "\033P\022\033W\000\033ln\033Qn\015\033U1\033t\000";
  59.     UBYTE *ptr, *ptrstart;
  60.     int i, err;
  61.  
  62.     switch(status) {
  63.         case 0 : /* Master Initialization */
  64.             /*
  65.                 ct    - pointer to IODRPReq structure.
  66.                 x    - width of printed picture in pixels.
  67.                 y    - height of printed picture in pixels.
  68.             */
  69.             RowSize = x;
  70.             ColorSize = RowSize + NUMTOTALCMD;
  71.             if (PD->pd_Preferences.PrintShade == SHADE_COLOR) {
  72.                 NumColorBufs = MAXCOLORBUFS;
  73.                 colors[0] = ColorSize * 3; /* Black */
  74.                 colors[1] = ColorSize * 0; /* Yellow */
  75.                 colors[2] = ColorSize * 1; /* Magenta */
  76.                 colors[3] = ColorSize * 2; /* Cyan */
  77.                 colorcodes[0] = 4; /* Yellow */
  78.                 colorcodes[1] = 1; /* Magenta */
  79.                 colorcodes[2] = 2; /* Cyan */
  80.                 colorcodes[3] = 0; /* Black */
  81.             }
  82.             else { /* grey-scale or black&white */
  83.                 NumColorBufs = 1;
  84.                 colors[0] = ColorSize * 0; /* Black */
  85.                 colorcodes[0] = 0; /* Black */
  86.             }
  87.             BufSize = ColorSize * NumColorBufs + NUMLFCMD;
  88.             if (PED->ped_YDotsInch == 216) {
  89.                 TwoBufSize = BufSize * 3;
  90.                 TotalBufSize = BufSize * 6;
  91.             }
  92.             else if (PED->ped_YDotsInch == 144) {
  93.                 TwoBufSize = BufSize * 2;
  94.                 TotalBufSize = BufSize * 4;
  95.             }
  96.             else {
  97.                 TwoBufSize = BufSize * 1;
  98.                 TotalBufSize = BufSize * 2;
  99.             }
  100.             PD->pd_PrintBuf = AllocMem(TotalBufSize, MEMF_PUBLIC);
  101.             if (PD->pd_PrintBuf == NULL) {
  102.                 err = PDERR_BUFFERMEMORY;
  103.             }
  104.             else {
  105.                 dataoffset = NUMSTARTCMD;
  106.                 /*
  107.                     This printer prints graphics within its
  108.                     text margins.  This code makes sure the
  109.                     printer is in 10 cpi and then sets the
  110.                     left and right margins to their minimum
  111.                     and maximum values (respectively).  A
  112.                     carriage return is sent so that the
  113.                     print head is at the leftmost position
  114.                     as this printer starts printing from
  115.                     the print head's position.  The printer
  116.                     is put into unidirectional mode to
  117.                     reduce wavy vertical lines.
  118.                 */
  119.                 StartBuf[PITCH] = 'P'; /* 10 cpi */
  120.                 StartBuf[CONDENSED] = '\022'; /* off */
  121.                 /* left margin of 1 */
  122.                 StartBuf[LMARG] = 0;
  123.                 /* right margin of 80 or 136 */
  124.                 StartBuf[RMARG] = PD->pd_Preferences.
  125.                     PaperSize == W_TRACTOR ? 136 : 80;
  126.                 /* uni-directional mode */
  127.                 StartBuf[DIREC] = '1';
  128.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  129.             }
  130.             break;
  131.  
  132.         case 1 : /* Scale, Dither and Render */
  133.             /*
  134.                 ct    - pointer to PrtInfo structure.
  135.                 x    - 0.
  136.                 y    - row # (0 to Height - 1).
  137.             */
  138.             Transfer(ct, y, &PD->pd_PrintBuf[dataoffset], colors,
  139.                 BufSize);
  140.             err = PDERR_NOERR; /* all ok */
  141.             break;
  142.  
  143.         case 2 : /* Dump Buffer to Printer */
  144.             /*
  145.                 ct    - 0.
  146.                 x    - 0.
  147.                 y    - # of rows sent (1 to NumRows).
  148.  
  149.             */
  150.             /* white-space strip */
  151.             ptrstart = &PD->pd_PrintBuf[dataoffset - NUMSTARTCMD];
  152.             if (PED->ped_YDotsInch == 72) {
  153.                 /* y range : 1 to 8 */
  154.                 y = y * 3 - spacing;
  155.                 ptr = CompactBuf(ptrstart + NUMSTARTCMD,
  156.                     ptrstart, y, 1);
  157.             }
  158.             else if (PED->ped_YDotsInch == 144) {
  159.                 /* y range : 1 to 16 */
  160.                 ptr = CompactBuf(ptrstart + NUMSTARTCMD,
  161.                     ptrstart, 2, 1);
  162.                 if (y > 1) {
  163.                     ptr = CompactBuf(&PD->pd_PrintBuf[
  164.                         dataoffset + BufSize],
  165.                         ptr, y * 3 / 2 - 2, 0);
  166.                 }
  167.             }
  168.             else if (PED->ped_YDotsInch == 216) {
  169.                 /* y range : 1 to 24 */
  170.                 ptr = CompactBuf(ptrstart + NUMSTARTCMD,
  171.                     ptrstart, 1, 1);
  172.                 if (y > 1) {
  173.                     ptr = CompactBuf(&PD->pd_PrintBuf[
  174.                         dataoffset + BufSize],
  175.                         ptr, 1, 0);
  176.                 }
  177.                 if (y > 2) {
  178.                     ptr = CompactBuf(&PD->pd_PrintBuf[
  179.                         dataoffset + BufSize * 2],
  180.                         ptr, y - 2, 0);
  181.                 }
  182.             }
  183.             err = (*(PD->pd_PWrite))(ptrstart, ptr - ptrstart);
  184.             if (err == PDERR_NOERR) {
  185.                 dataoffset = (dataoffset == NUMSTARTCMD ?
  186.                     TwoBufSize : 0) + NUMSTARTCMD;
  187.             }
  188.             break;
  189.  
  190.         case 3 : /* Clear and Init Buffer */
  191.             /*
  192.                 ct    - 0.
  193.                 x    - 0.
  194.                 y    - 0.
  195.             */
  196.             ClearAndInit(&PD->pd_PrintBuf[dataoffset]);
  197.             err = PDERR_NOERR;
  198.             break;
  199.  
  200.         case 4 : /* Close Down */
  201.             /*
  202.                 ct    - error code.
  203.                 x    - io_Special flag from IODRPReq.
  204.                 y    - 0.
  205.             */
  206.             err = PDERR_NOERR; /* assume all ok */
  207.             /* if user did not cancel print */
  208.             if (ct != PDERR_CANCEL) {
  209.                 /* restore preferences pitch and margins */
  210.                 if (PD->pd_Preferences.PrintPitch == ELITE) {
  211.                     StartBuf[PITCH] = 'M'; /* 12 cpi */
  212.                 }
  213.                 else if (PD->pd_Preferences.PrintPitch == FINE) {
  214.                     StartBuf[CONDENSED] = '\017'; /* on */
  215.                 }
  216.                 StartBuf[LMARG] =
  217.                     PD->pd_Preferences.PrintLeftMargin - 1;
  218.                 StartBuf[RMARG] =
  219.                     PD->pd_Preferences.PrintRightMargin;
  220.                 StartBuf[DIREC] = '0'; /* bi-directional */
  221.                 err = (*(PD->pd_PWrite))(StartBuf, STARTLEN);
  222.             }
  223.             (*(PD->pd_PBothReady))();
  224.             if (PD->pd_PrintBuf != NULL) {
  225.                 FreeMem(PD->pd_PrintBuf, TotalBufSize);
  226.             }
  227.             break;
  228.  
  229.         case 5 :  /* Pre-Master Initialization */
  230.             /*
  231.                 ct    - 0 or pointer to IODRPReq structure.
  232.                 x    - io_Special flag from IODRPReq.
  233.                 y    - 0.
  234.             */
  235.             /* kludge for sloppy tractor mechanism */
  236.             spacing = PD->pd_Preferences.PaperType == SINGLE ?
  237.                 1 : 0;
  238.             dpi_code = SetDensity(x & SPECIAL_DENSITYMASK);
  239.             err = PDERR_NOERR;
  240.             break;
  241.     }
  242.     return(err);
  243. }
  244.  
  245. UBYTE *CompactBuf(ptrstart, ptr2start, y, flag)
  246. UBYTE *ptrstart, *ptr2start;
  247. long y;
  248. int flag; /* 0 - not first pass, !0 - first pass */
  249. {
  250.     static int x;
  251.     UBYTE *ptr, *ptr2;
  252.     long ct;
  253.     int i;
  254.  
  255.     ptr2 = ptr2start; /* where to put the compacted data */
  256.     if (flag) {
  257.         x = 0; /* flag no transfer required yet */
  258.     }
  259.     for (ct=0; ct<NumColorBufs; ct++, ptrstart += ColorSize) {
  260.         i = RowSize;
  261.         ptr = ptrstart + i - 1;
  262.         while (i > 0 && *ptr == 0) {
  263.             i--;
  264.             ptr--;
  265.         }
  266.         if (i != 0) { /* if data */
  267.             *(++ptr) = 13;            /* <cr> */
  268.             ptr = ptrstart - NUMSTARTCMD;
  269.             *ptr++ = 27;
  270.             *ptr++ = 'r';
  271.             *ptr++ = colorcodes[ct];    /* color */
  272.             *ptr++ = 27;
  273.             *ptr++ = dpi_code;        /* density */
  274.             *ptr++ = i & 0xff;
  275.             *ptr++ = i >> 8;        /* size */
  276.             i += NUMTOTALCMD;
  277.             if (x != 0) { /* if must transfer data */
  278.                 /* get src start */
  279.                 ptr = ptrstart - NUMSTARTCMD;
  280.                 do { /* transfer and update dest ptr */
  281.                     *ptr2++ = *ptr++;
  282.                 } while (--i);
  283.             }
  284.             else { /* no transfer required */
  285.                 ptr2 += i; /* update dest ptr */
  286.             }
  287.         }
  288.         if (i != RowSize + NUMTOTALCMD) { /* if compacted or 0 */
  289.             x = 1; /* flag that we need to transfer next time */
  290.         }
  291.     }
  292.     *ptr2++ = 13; /* cr */
  293.     *ptr2++ = 27;
  294.     *ptr2++ = 'J';
  295.     *ptr2++ = y; /* y/216 lf */
  296.     return(ptr2);
  297. }
  298.  
  299. ClearAndInit(ptr)
  300. UBYTE *ptr;
  301. {
  302.     ULONG *lptr, i, j;
  303.  
  304.     /*
  305.         Note : Since 'NUMTOTALCMD + NUMLFCMD' is > 3 bytes if is safe
  306.         to do the following to speed things up.
  307.     */
  308.     i = TwoBufSize - NUMTOTALCMD - NUMLFCMD;
  309.     j = (ULONG)ptr;
  310.     if (!(j & 1)) { /* if on a word boundary, clear by longs */
  311.         i = (i + 3) / 4;
  312.         lptr = (ULONG *)ptr;
  313.         do {
  314.             *lptr++ = 0;
  315.         } while (--i);
  316.     }
  317.     else { /* clear by bytes */
  318.         do {
  319.             *ptr++ = 0;
  320.         } while (--i);
  321.     }
  322. }
  323.